<?php

#error_reporting(E_ALL);
require_once($_SERVER['DOCUMENT_ROOT'].'../system/config.php');
require_once $INCLUDE_DIR.'cametrics.helper.php';

$logfile = $BASE_DIR.'../log/campaignrlocation.php';

$client = new SoapClient($INCLUDE_DIR."sensorbase.wsdl", array("trace"=>1,"exceptions"=>0));
$un = $SB_USER;
$pwd = $SB_PASSWORD;
$project_id = $SB_PROJECT_ID;
$table_name = $SB_TABLE;

echo "Data successfully slogged!\n";

$xml_str = stripslashes($_POST['data_string']);
$returned_msg = $client->slogDataXML($un, $pwd, $project_id, $table_name, $xml_str);

#file_put_contents('campaignrflickr.log', $xml_str."\r\n", FILE_APPEND);

$xml_str = preg_replace("/field name=\"([^\"]+)\">([^<]+)?<\/field>/", '$1>$2</$1>', $xml_str);
$data = simplexml_load_string($xml_str);

file_put_contents($logfile, print_r($data, true)."\r\n", FILE_APPEND);

#ob_start();
#var_dump($data);
#$data = ob_get_clean();
#file_put_contents('campaignrflickr.log', "olmosucks".$data."\r\n\r\n", FILE_APPEND);

exit();
/*
// open database
$db = null;

try {
	$dbname = dirname(__FILE__).'/../databases/whatsinvasive.db';
	$db = new PDO("sqlite:".$dbname);
} catch (PDOException $exception) {
	die("Failed to connect to the  database. Error: %s".$exception->getMessage());
}


foreach($data->row as $field)
{

	$timestamp = $field->timestamp;
	$location = $field->location;
	$imei = $field->imei;

	$location = str_replace("NaN",'""',$location);
	$location = json_decode($location);

	$latitude = $location->latitude;
	$longitude = $location->longitude;

	list($day, $other) = split('T', $timestamp);
        $time = substr($other, 0, 8);

	if (!isset($imei) || $imei == '') {
		continue;
	}

	$result = $db->query("SELECT username, usergroup FROM user WHERE imei=$imei");
	if (!$result) {
		$username = $imei;
		$groupname = 'group_'.$imei;
	}

	$row = $result->fetch(PDO::FETCH_ASSOC);

	if (!$row || count($row) < 2) {
		$username = $imei;
		$groupname = 'group_'.$imei;
	}
	else {
		$username = $row['username'];
		$groupname = $row['usergroup'];
	}

	if(($latitude != "") and ($longitude != "") and ($latitude != "NaN") and ($longitude != "NaN"))
	{

	        #Cametrics::measure($imei.".movement.location", $longitude.",".$latitude, 'location');
        	#Cametrics::measure($imei.".movement.time", $day.' '.$time, 'datetime');


		Cametrics::measure($username.".movement.all", $longitude.",".$latitude, 'location');
        	Cametrics::measure($username.".movement.time", $day.' '.$time, 'datetime');
		Cametrics::measure($groupname.".movement.all", $longitude.",".$latitude, 'location');
        	Cametrics::measure($groupname.".movement.time", $day.' '.$time, 'datetime');
		Cametrics::measure("movement.all", $longitude.",".$latitude, 'location');
        	Cametrics::measure("movement.time", $day.' '.$time, 'datetime');

		Cametrics::measure("$groupname.movement.$username.all", $longitude.",".$latitude, 'location');
        	Cametrics::measure("$groupname.movement.$username.time", $day.' '.$time, 'datetime');
		Cametrics::measure("movement.$username.all", $longitude.",".$latitude, 'location');
        	Cametrics::measure("movement.$username.time", $day.' '.$time, 'datetime');


	}
}

$db = null;
*/
?>
